/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.modules.repair.entity.BuildingFloorElevatorNum;
import org.springblade.modules.repair.entity.Elevator;
import org.springblade.modules.repair.service.IElevatorService;
import org.springblade.modules.repair.vo.BuildingFloorElevatorNumVO;
import org.springblade.modules.repair.mapper.BuildingFloorElevatorNumMapper;
import org.springblade.modules.repair.service.IBuildingFloorElevatorNumService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.repair.vo.elevator.ElevatorFloorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 楼栋-层数表 服务实现类
 *
 * @author ysq
 * @since 2024-08-07
 */
@Service
public class BuildingFloorElevatorNumServiceImpl extends BaseServiceImpl<BuildingFloorElevatorNumMapper, BuildingFloorElevatorNum> implements IBuildingFloorElevatorNumService {

	@Autowired
	private IElevatorService elevatorService;

	@Override
	public IPage<BuildingFloorElevatorNumVO> selectBuildingFloorElevatorNumPage(IPage<BuildingFloorElevatorNumVO> page, BuildingFloorElevatorNumVO buildingFloorElevatorNum) {
		return page.setRecords(baseMapper.selectBuildingFloorElevatorNumPage(page, buildingFloorElevatorNum));
	}

	@Override
	public void saveList(List<BuildingFloorElevatorNum> numLists, Long id) {
//第一步 过滤删除的数据
		List<BuildingFloorElevatorNum> elevatorNums = this.list(Wrappers.<BuildingFloorElevatorNum>lambdaQuery().eq(BuildingFloorElevatorNum::getFloorElevatorId, id));
		List<Long> searchIds = numLists.stream().filter(o -> o.getId() != null).map(BuildingFloorElevatorNum::getId).collect(Collectors.toList());
		List<Long> deleteIds = elevatorNums.stream().filter(o -> !searchIds.contains(o.getId())).map(BuildingFloorElevatorNum::getId).collect(Collectors.toList());
		if (deleteIds.size() > 0) {
			this.removeByIds(deleteIds);
		}
		//第一步 保存更新
		numLists.stream().forEach(o -> o.setFloorElevatorId(id));
		this.saveOrUpdateBatch(numLists);
		numLists = this.list(Wrappers.<BuildingFloorElevatorNum>lambdaQuery().eq(BuildingFloorElevatorNum::getFloorElevatorId, id));
		List<Elevator> list = elevatorService.list(Wrappers.<Elevator>lambdaQuery().eq(Elevator::getFloorElevatorId, id));
		for (Elevator elevator : list) {
			//层处理
			List<ElevatorFloorVo> floorTier = JSON.parseArray(JSON.toJSONString(elevator.getFloorTier()), ElevatorFloorVo.class);
			List<ElevatorFloorVo> floorTier1 = new ArrayList<>(16);
			if (floorTier == null || floorTier.size() == 0) {
				for (BuildingFloorElevatorNum buildingFloorElevatorNum : numLists) {
					ElevatorFloorVo elevatorFloorVo = new ElevatorFloorVo();
					elevatorFloorVo.setId(buildingFloorElevatorNum.getId());
					elevatorFloorVo.setFloorNum(buildingFloorElevatorNum.getFloorNum());
					elevatorFloorVo.setIsHas(false);
					floorTier1.add(elevatorFloorVo);
				}
			} else {
				List<Long> ids = numLists.stream().map(BuildingFloorElevatorNum::getId).collect(Collectors.toList());
				List<ElevatorFloorVo> voList = floorTier.stream().filter(o -> ids.contains(o.getId())).collect(Collectors.toList());
				Map<Long, List<ElevatorFloorVo>> listMap = voList.stream().collect(Collectors.groupingBy(ElevatorFloorVo::getId));
				for (BuildingFloorElevatorNum buildingFloorElevatorNum : numLists) {
					List<ElevatorFloorVo> elevatorFloorVos = listMap.get(buildingFloorElevatorNum.getId());
					if (elevatorFloorVos == null || elevatorFloorVos.size() == 0) {
						ElevatorFloorVo elevatorFloorVo = new ElevatorFloorVo();
						elevatorFloorVo.setId(buildingFloorElevatorNum.getId());
						elevatorFloorVo.setFloorNum(buildingFloorElevatorNum.getFloorNum());
						elevatorFloorVo.setIsHas(false);
						floorTier1.add(elevatorFloorVo);
					} else {
						ElevatorFloorVo elevatorFloorVo = elevatorFloorVos.get(0);
						elevatorFloorVo.setFloorNum(buildingFloorElevatorNum.getFloorNum());
						floorTier1.add(elevatorFloorVo);
					}
				}
			}

			//站处理
			List<ElevatorFloorVo> floorDoors = JSON.parseArray(JSON.toJSONString(elevator.getFloorDoors()), ElevatorFloorVo.class);
			List<ElevatorFloorVo> floorDoors1 = new ArrayList<>(16);
			if (floorDoors == null || floorDoors.size() == 0) {
				for (BuildingFloorElevatorNum buildingFloorElevatorNum : numLists) {
					ElevatorFloorVo elevatorFloorVo = new ElevatorFloorVo();
					elevatorFloorVo.setId(buildingFloorElevatorNum.getId());
					elevatorFloorVo.setFloorNum(buildingFloorElevatorNum.getFloorNum());
					elevatorFloorVo.setIsHas(false);
					floorDoors1.add(elevatorFloorVo);
				}
			} else {
				List<Long> ids = numLists.stream().map(BuildingFloorElevatorNum::getId).collect(Collectors.toList());
				List<ElevatorFloorVo> voList = floorDoors.stream().filter(o -> ids.contains(o.getId())).collect(Collectors.toList());
				Map<Long, List<ElevatorFloorVo>> listMap = voList.stream().collect(Collectors.groupingBy(ElevatorFloorVo::getId));
				for (BuildingFloorElevatorNum buildingFloorElevatorNum : numLists) {
					List<ElevatorFloorVo> elevatorFloorVos = listMap.get(buildingFloorElevatorNum.getId());
					if (elevatorFloorVos == null || elevatorFloorVos.size() == 0) {
						ElevatorFloorVo elevatorFloorVo = new ElevatorFloorVo();
						elevatorFloorVo.setId(buildingFloorElevatorNum.getId());
						elevatorFloorVo.setFloorNum(buildingFloorElevatorNum.getFloorNum());
						elevatorFloorVo.setIsHas(false);
						floorDoors1.add(elevatorFloorVo);
					} else {
						ElevatorFloorVo elevatorFloorVo = elevatorFloorVos.get(0);
						elevatorFloorVo.setFloorNum(buildingFloorElevatorNum.getFloorNum());
						floorDoors1.add(elevatorFloorVo);
					}
				}
			}

			//门
			List<ElevatorFloorVo> floorPlatforms = JSON.parseArray(JSON.toJSONString(elevator.getFloorPlatforms()), ElevatorFloorVo.class);
			List<ElevatorFloorVo> floorPlatforms1 = new ArrayList<>(16);
			if (floorPlatforms == null || floorPlatforms.size() == 0) {
				for (BuildingFloorElevatorNum buildingFloorElevatorNum : numLists) {
					ElevatorFloorVo elevatorFloorVo = new ElevatorFloorVo();
					elevatorFloorVo.setId(buildingFloorElevatorNum.getId());
					elevatorFloorVo.setFloorNum(buildingFloorElevatorNum.getFloorNum());
					elevatorFloorVo.setIsHas(false);
					floorPlatforms1.add(elevatorFloorVo);
				}
			} else {
				List<Long> ids = numLists.stream().map(BuildingFloorElevatorNum::getId).collect(Collectors.toList());
				List<ElevatorFloorVo> voList = floorPlatforms.stream().filter(o -> ids.contains(o.getId())).collect(Collectors.toList());
				Map<Long, List<ElevatorFloorVo>> listMap = voList.stream().collect(Collectors.groupingBy(ElevatorFloorVo::getId));
				for (BuildingFloorElevatorNum buildingFloorElevatorNum : numLists) {
					List<ElevatorFloorVo> elevatorFloorVos = listMap.get(buildingFloorElevatorNum.getId());
					if (elevatorFloorVos == null || elevatorFloorVos.size() == 0) {
						ElevatorFloorVo elevatorFloorVo = new ElevatorFloorVo();
						elevatorFloorVo.setId(buildingFloorElevatorNum.getId());
						elevatorFloorVo.setFloorNum(buildingFloorElevatorNum.getFloorNum());
						elevatorFloorVo.setIsBackDoor(false);
						elevatorFloorVo.setIsFrontDoor(false);
						floorPlatforms1.add(elevatorFloorVo);
					} else {
						ElevatorFloorVo elevatorFloorVo = elevatorFloorVos.get(0);
						elevatorFloorVo.setFloorNum(buildingFloorElevatorNum.getFloorNum());
						floorPlatforms1.add(elevatorFloorVo);
					}
				}
			}
			elevator.setFloorTier(floorTier1);
			elevator.setFloorDoors(floorDoors1);
			elevator.setFloorPlatforms(floorPlatforms1);
		}
		elevatorService.updateBatchById(list);
	}

}
